<template>
    <div class="user-card">
      <div class="avatar-section" @click="toggleDetails">
        <el-avatar :src="user.avatar" size="large" class="user-avatar">
          <!-- 如果头像加载失败，可以使用用户名的首字母作为备用 -->
          <img
            :src="user.avatar"
            alt="avatar"
            @error="handleAvatarError($event)"
          />
          <span v-if="!user.avatar">{{ user.username?.charAt(0).toUpperCase() }}</span>
        </el-avatar>
        <div class="details-section" v-if="isDetailsVisible">
          <p class="user-email">邮箱: {{ user.email }}</p>
          <p class="user-phone">电话: {{ user.phone }}</p>
          <el-icon class="delete-icon" @click.stop="handleDeleteUser">
            <Delete />
          </el-icon>
        </div>
      </div>
      <span class="username">{{ user.username }}</span>
    </div>
  </template>
  
  <script lang="ts" setup>
  import { ref } from 'vue';
  import { ElAvatar, ElIcon } from 'element-plus';
  import { Delete } from '@element-plus/icons-vue';
  import type { User } from '../../type/User';
  
  // 定义组件的 props
  const props = defineProps<{
    user: User;
  }>();
  
  // 定义组件的 emits
  const emit = defineEmits<{
    (e: 'delete-user', userId: string): void;
  }>();
  
  // 状态变量，用于控制详情的显示
  const isDetailsVisible = ref(false);
  
  /**
   * 处理头像加载错误，隐藏图片并显示用户名首字母
   * @param event 图片加载错误事件
   */
  const handleAvatarError = (event: Event) => {
    const imgElement = event.target as HTMLImageElement;
    imgElement.style.display = 'none';
  };
  
  /**
   * 切换详情显示状态
   */
  const toggleDetails = () => {
    isDetailsVisible.value = !isDetailsVisible.value;
  };
  
  /**
   * 处理删除用户事件
   */
  const handleDeleteUser = () => {
    emit('delete-user', props.user.userId||'');
  };
  </script>
  
  <style lang="scss" scoped>
  .user-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 150px;
    padding: 10px;
   // border: 1px solid #ebeef5;
   // border-radius: 8px;
    background-color: #fff;
    position: relative;
    transition: box-shadow 0.3s;
  
    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
  }
  
  .avatar-section {
    position: relative;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }
  
  .user-avatar {
    cursor: pointer;
  }
  
  .details-section {
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    width: 220px;
    padding: 10px;
    margin-top: 10px;
    background-color: #ffffff;
    //border: 1px solid #ebeef5;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    z-index: 10;
  
    .user-email,
    .user-phone {
      margin: 5px 0;
      font-size: 14px;
      color: #606266;
    }
  
    .delete-icon {
      position: absolute;
      top: 10px;
      right: 10px;
      color: red;
      cursor: pointer;
      font-size: 18px;
    }
  }
  
  .username {
    margin-top: 10px;
    font-weight: bold;
    font-size: 16px;
    color: #303133;
    text-align: center;
    word-break: break-all;
  }
  </style>
  